<?php
namespace Home\Controller;

use Home\Event\JssdkEvent;
use Home\Event\WeixinEvent;
use Home\Event\WeixinWebEvent;
use Think\Controller;


class BaseController extends Controller {
    protected $_arr;
    public $page_num;
    public $wx_obj;
    public $member_data;
    public function __construct(){
        parent::__construct();

        // TODO 升级系统需要修改这里
//        $this->display('Public/upgrade');
//        exit;

        $openId = session('wx_openid');
        // TODO 注释的是微信打开
        if(!$openId){
            $getUrl = $this->isaccredit();
            redirect($getUrl);
        }

        /*else{
           $u_data = M('b2c_members')->where(['openid'=>$openId])->find();
           session('userInfo',$u_data);
        }*/

        $this->member_data = session('userInfo');
        $member_id = $this->member_data['member_id'];
        $this->assign('wx_member_id',$member_id);
        //  默认店铺
        $default_local_id = session('t_userInfo')['default_local_id'];
        if ($default_local_id == '21') {
            redirect('/Home/First/stortSelect');
        }
        $local_id = I('local_id');
//        if ($local_id == 21 || $default_local_id != $local_id) {
//            redirect('/Home/First/stortSelect');
//        }

        if ($local_id) {
            $local_store = M("webpos_local_store")->where(['local_id' => $local_id, 'local_status' => '0'])->find();

            if ($local_store['local_id'] == $local_id) {
                redirect('/Home/First/stortSelect');
            }
        }

        $this->assign('default_local_id',$default_local_id);
        //logo图片（分享）
        $weixin_log =M('weixin_share_logo')->where(['local_id'=>$default_local_id])->find();
        $this->assign('weixin_logo',$weixin_log);
        //微信js配置
        $this->wxsdk();
        //$this->assign('app_id',C('APPID'));


    }

    /**
     * 授权
     */
    public function isaccredit(){
        $data = get_url();
        $getUrl = $data['top'].$data['host'].$data['parameter'];
//        file_put_contents('a.txt', $getUrl);
        $getUrl = urlencode($getUrl);
        $url = $data['top'].$data['host'].'/Home/First/publicBase?return_url='.$getUrl;
//        file_put_contents('b.txt', $url);
        $url = urlencode($url);
        $getUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('APPID')."&redirect_uri=".$url."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//        echo $getUrl;exit;
        return $getUrl;
    }
    /**
     * 微信js配置
     */
    public function wxsdk(){
        $jssdk = new JssdkEvent();
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage',$signPackage);
    }
    /**
     * 判断用户权限
     */
    public function power(){

    }

    public function navMenu(){

    }

    public function __destruct(){
        parent::__destruct();
    }



}